<?php

namespace App\Console\Commands;

use App\BookApply;
use App\Document;
use App\Jobs\MoveLocalFileToOssJob;
use App\PatentApply;
use App\PatentAssess;
use App\PlantVariety;
use App\Transverse;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;

class MoveLocalFileToOssCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:move-file {from} {to}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $from = $this->argument('from');
        $to = $this->argument('to');
        $this->copy($from, $to);
        echo "all end-----------------------\n ";
    }

    protected function copy($from, $to)
    {
        BookApply::query()->select(['file', 'cert'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($from, $to) {
                $list->map(function ($model) use ($from, $to) {
                    $this->copyField($from, $to, $model->toArray());
                });
            });
        echo "【BookApply】end-----------------------\n ";

        PatentApply::query()->select(['file', 'cert', 'retrieval_report'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($from, $to) {
                $list->map(function ($model) use ($from, $to) {
                    $this->copyField($from, $to, $model->toArray());
                });
            });
        echo "【PatentApply】end-----------------------\n ";

        PlantVariety::query()->select(['cert'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($from, $to) {
                $list->map(function ($model) use ($from, $to) {
                    $this->copyField($from, $to, $model->toArray());
                });
            });
        echo "【PlantVariety】end-----------------------\n ";

        Transverse::query()->select(['file', 'file2'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($from, $to) {
                $list->map(function ($model) use ($from, $to) {
                    $this->copyField($from, $to, $model->toArray());
                });
            });
        echo "【Transverse】end-----------------------\n ";

        PatentAssess::query()->select(['path'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($from, $to) {
                $list->map(function ($model) use ($from, $to) {
                    $this->copyField($from, $to, $model->toArray());
                });
            });
        echo "【PatentAssess】end-----------------------\n ";

        Document::query()->select(['path'])
            ->orderBy('id')
            ->chunk(100, function ($list) use ($from, $to) {
                $list->map(function ($model) use ($from, $to) {
                    $this->copyField($from, $to, $model->toArray());
                });
            });
        echo "【Document】end-----------------------\n ";
    }

    protected function copyField($from, $to, $fields)
    {
        foreach ($fields as $path) {
            if ($path) {
                dispatch(new MoveLocalFileToOssJob($from, $to, $path));
            }
        }
    }
}
